﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Water.Core;
using Water.Data.Infrastructure;
using Water.Model;
using NHibernate.Linq;
using System.Linq.Expressions;

namespace Water.Data.Repositories
{
    public class FlushWaterAccountRepository: NhRepositoryBase<FlushWaterAccount>
    {
        private readonly IUnitOfWork uow;

        public FlushWaterAccountRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="mr">实体</param>
        /// <returns>bool</returns>
        public bool Save(FlushWaterAccount mr)
        {
            if (Add(mr))
            {
                uow.Commit();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="mr">实体</param>
        /// <returns>bool</returns>
        public bool Edit(FlushWaterAccount mr)
        {
            if (Update(mr))
            {
                uow.Commit();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="code">编号</param>
        /// <returns></returns>
        public FlushWaterAccount Get(string code)
        {
            return session.Query<FlushWaterAccount>().Where(e => e.MRNO == code)
                .Fetch(e=>e.Pl).SingleOrDefault();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="code">编号</param>
        /// <returns></returns>
        public bool Delete(string code)
        {
            try
            {
                var count = session.CreateQuery("delete from FlushWaterAccount m where m.MRNO=?")
                                  .SetString(0, code)
                                   .ExecuteUpdate();
                uow.Commit();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<FlushWaterAccount> LoadAll(Expression<Func<FlushWaterAccount, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<FlushWaterAccount>[] orderByExpressions)
        {
            var query = GetAll().Where(where);
            if (orderByExpressions != null && orderByExpressions.Any())
            {
                query = query.OrderBy(orderByExpressions);
            }
            query = query.Fetch(e => e.Pl);
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            
            return query.ToList();
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadCount(Expression<Func<FlushWaterAccount, bool>> where)
        {
            var query = GetMany(where);
            return query.Count();
        }


    }
}
